﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

namespace Nirvana
{
    [CustomEditor(typeof(UIEventTable))]
    public sealed class UIEventTableEditor : Editor
    {
        private SerializedProperty variablesProp;
        private ReorderableList reorderableList;
        private HashSet<int> variableIndexHash = new HashSet<int>();
        private Dictionary<string, int> variableNameMap = new Dictionary<string, int>();
        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            reorderableList.DoLayoutList();
            if (serializedObject.ApplyModifiedProperties())
            {
                ReLoad();
            }
            UIEventTable table = (UIEventTable)this.target;
            if (GUILayout.Button("Sort", new GUILayoutOption[0]))
            {
                Undo.RecordObject(table, "Sort Bind Table");
                base.serializedObject.Update();
                table.Sort();
                base.serializedObject.ApplyModifiedProperties();
            }
        }

        void OnEnable()
        {
            if (this.target == null) return;
            variablesProp = serializedObject.FindProperty("events");
            reorderableList = new ReorderableList(serializedObject, variablesProp);
            reorderableList.drawElementCallback = DrawNameElement;//绘制每个元素的回调
            reorderableList.drawHeaderCallback = DrawHeader;//绘制列表表头
            reorderableList.elementHeightCallback = OnElementHeightCallback;
            ReLoad();
        }
        private void DrawHeader(Rect rect)
        {
            GUI.Label(rect, "Events:");
        }
        private float OnElementHeightCallback(int index)
        {
            SerializedProperty prop = this.variablesProp.GetArrayElementAtIndex(index);
            if (!prop.isExpanded)//此元素是否展开
            {
                return  EditorGUIUtility.singleLineHeight;
            }
            UIEventTable table = (UIEventTable)this.target;
            var variablellist = table.FindReferenced(prop.stringValue);
            if (variablellist == null)
            {
                return EditorGUIUtility.singleLineHeight;
            }

            return (float)(1 + variablellist.Count) * EditorGUIUtility.singleLineHeight;
        }

        private void DrawNameElement(Rect rect, int index, bool isActive, bool isFocused)
        {
            SerializedProperty prop = this.variablesProp.GetArrayElementAtIndex(index);
            bool flag = this.variableIndexHash.Contains(index);
            Color color = GUI.color;
            if (flag) GUI.color = new Color(1f, 0.5f, 0.5f, 1);

            Rect r0 = new Rect(rect.x + 8f, rect.y, 16f, EditorGUIUtility.singleLineHeight);
            Rect r1 = new Rect(rect.x + 12f, rect.y, (rect.width - 12f), EditorGUIUtility.singleLineHeight);

            prop.isExpanded = EditorGUI.Foldout(r0, prop.isExpanded, GUIContent.none);//三角按键
            EditorGUI.PropertyField(r1, prop, GUIContent.none);

            if (prop.isExpanded)
            {
                UIEventTable table = (UIEventTable)this.target;
                var variable = table.FindReferenced(prop.stringValue);
                if (variable != null)
                {
                    GUI.enabled = false;
                    Rect r4 = new Rect(rect.x + 12f, rect.y, rect.width - 12f, EditorGUIUtility.singleLineHeight);
                    foreach (var p in variable)
                    {
                        r4.y += EditorGUIUtility.singleLineHeight;
                        EditorGUI.ObjectField(r4, p, p.GetType(), true);
                    }
                    GUI.enabled = true;
                }
            }
            GUI.color = color;
        }

        private void ReLoad()
        {
            variableIndexHash.Clear();
            variableNameMap.Clear();
            for (int i = 0; i < this.variablesProp.arraySize; i++)
            {
                var element = this.variablesProp.GetArrayElementAtIndex(i);
                if (this.variableNameMap.ContainsKey(element.stringValue))
                {
                    this.variableIndexHash.Add(this.variableNameMap[element.stringValue]);
                    this.variableIndexHash.Add(i);
                }
                else
                {
                    this.variableNameMap.Add(element.stringValue, i);
                }
            }
        }
    


    }
}

